<?php 
namespace Cli\Controller;
use \Think\Controller;
/**
 * 把原来hotelDetail表的个性推荐信息 插入到新表hotelspecial中 并设置为掌柜说 即设置isactive = 1
 *@modify wenhui 2016-10-28 创建
 */
class SpecialController extends Controller {
    /**
     * 把原来hotelDetail表的个性推荐信息 插入到新表hotelspecial中
     * 并设置为掌柜说
     * 
     */
    public function special() {
    	if (!IS_CLI) {           
            exit();
        }

    	$hotelSpecialModel = D('HotelSpecial');
        $hotelDetailModel = D('HotelDetail');
        $hotelDetailInfo = $hotelDetailModel->select();
        if (empty($hotelDetailInfo)) {
        	echo "failed\n";
        	return false;
        }
        // 获取数据 组装格式
        $specialData = array();
        foreach ($hotelDetailInfo as &$hotelDetailItem) {
            $hotelId = $hotelDetailItem['hotelentity_id'];
            $special = $hotelDetailItem['special'];
            $specialimageurl = $hotelDetailItem['specialimageurl'];
            $creator = $hotelDetailItem['creator'];
            $operator = $hotelDetailItem['operator'];
            $arrayItem = array(
                'hotelentity_id' => $hotelId,
                'special' => $special,
                'specialimageurl' => $specialimageurl,
                'creator' => $creator,
                'operator' => $operator,
                'isactive' => ENABLE,
            );
            array_push($specialData, $arrayItem);
        }
        unset($hotelDetailItem);
		
        // 插入数据      
        $addResult = $hotelSpecialModel->addAll($specialData);
        
        if (empty($addResult)) {
        	echo "failed\n";
        	return false;
        }
    }

    /**
     * 补充hotelinfo表数据
     */
    public function addHotelInfo() {
        if (!IS_CLI) {
            exit();
        }

        $hotelEntityModel = D('HotelEntity');
        $hotelInfoModel = D('HotelInfo');
        $hotelIds = $hotelEntityModel->getAllId();
        $hotelInfoIds = $hotelInfoModel->getHotelIds();
        $hotelIds = array_column($hotelIds, 'id');
        $hotelInfoIds = array_column($hotelInfoIds, 'hotelentity_id');

        $diffHotelId = array_unique(array_diff($hotelIds, $hotelInfoIds));
        if (!empty($diffHotelId)) {
            //开启事务处理
            $hotelInfoModel->startTrans();
            $commit = true;
            $errorHotelId = 0;
            foreach ($diffHotelId as $key => $hotelId) {
                $Result = $hotelInfoModel->getInfoByHotel($hotelId);
                if (empty($Result)) {
                    $addResult = $hotelInfoModel->addInfo($hotelId);
                    if ($addResult === false) {
                        $hotelInfoModel->rollback();
                        $commit = false;
                        $errorHotelId = $hotelId;
                        break;
                    }
                }
            }
            unset($hotelId);

            if ($commit) {
                $hotelInfoModel->commit();
                echo ('success') . "\n";
                return;
            } else {
                echo ('fail hotelId = ' . $errorHotelId) . "\n";
                return;
            }
        }

        echo ('empty hotelId') . "\n";

        return;
    }
}